const uniID = require('uni-id')

const db = uniCloud.database()
const dbCmd = db.command
const $ = dbCmd.aggregate

const getUsersInfo = async (params) => {
	const multi = []
	
	params.ids.forEach(id => {
		multi.push($.eq(['$user_id', id]))
	})

	return await db.collection('uni-id-users').where(
		dbCmd.expr(
			$.or(
				multi
			)
		)
	).field({
		user_id: true,
		avatar: true,
		gender: true,
		nickname: true,
	}).get()
}

const getUser = (params) => {
	const list = db.collection('uni-id-users').where({
		_id: params._id
	}).get()

	return list[0]
}

const updateUserData = params => {
	return db.collection('uni-id-users').where({
		_id: params.uid
	}).update({
		username: params.nickname,
		nickname: params.nickname,
		avatar: params.avatar,
		gender: params.gender,
	})
}

module.exports = {
	getUser,
	updateUserData,
	getUsersInfo,
}